package com.turato.json.myjson;

import java.lang.reflect.Array;
import java.util.Collection;


/**
 * @author tuhao
 * @desc json字符串工具类
 */
public class StringUtils {

    /**
     * 转换成 json 字符串
     * @param object
     * @return
     */
    public static String toJsonString(Object object) {
        if (object == null) {
            return "null";
        }
        if (object.getClass().isArray()) {
            JsonArray jsonArray = new JsonArray();
            for (int i = 0; i < Array.getLength(object); i++) {
                jsonArray.add(Array.get(object, i));
            }
            return jsonArray.toString();
        }
        if (object instanceof Collection) {
            JsonArray jsonArray = new JsonArray();
            jsonArray.addAll((Collection<?>) object);
            return jsonArray.toString();
        }
        if (object instanceof String) {
            //给字符串加上双引号，字符串内有双引号的要用斜杠转义
            return parseString((String)object);
        }
        return object.toString();
    }

    public static String parseString(String str){
       return  "\"" + str.replace("\"", "\\\"") + "\"";
    }


    public static String toUnicodeString(String str) {
        if (str == null) {
            return null;
        }
        if (str.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= ' ' && c <= '~') {   //ascii可打印字符 32~126
                sb.append(c);
            } else {
                sb.append("\\u").append(leftPad(Integer.toHexString(c), 4));
            }
        }
        return sb.toString();
    }

    public static String leftPad(String str, int length) {
        if (str == null) {
            return null;
        }
        if (str.length() >= length) {
            return str;
        }
        StringBuilder sb = new StringBuilder(length);
        for (int i = str.length(); i < length; i++) {
            sb.append('0');
        }
        sb.append(str);
        return sb.toString();
    }


    public static String asciiToNative(String asciicode) {
        String[] asciis = asciicode.split("\\\\u");
        StringBuilder nativeValue = new StringBuilder(asciis[0]);

        for (int i = 1; i < asciis.length; i++) {
            String code = asciis[i];
            try {
                nativeValue.append((char) Integer.parseInt(code.substring(0, 4), 16));
                if (code.length() > 4) {
                    nativeValue.append(code.substring(4, code.length()));
                }
            } catch (StringIndexOutOfBoundsException ex) {
                throw new JsonException("invalid unicode string :" + code, ex);
            } catch (NumberFormatException ee) {
                throw new JsonException("invalid unicode string :" + code, ee);
            }
        }

        return nativeValue.toString();
    }

    private static String quoteString(String str){
        return "\""+str+"\"";
    }

    private static String replaceQuote(String str){
        return str.replace("\"","\\\"");
    }

    private static String jsonString(String str){
        return quoteString(replaceQuote(str));
    }


   public static String removeQuote(String str){
        if(str.length()==0){
            return str;
        }
        if(str.length()==1){
            return str.charAt('0')=='\"'?"":str;
        }
        int l = str.charAt(0)=='\"'?1:0;
        int r = str.charAt(str.length()-1)=='\"'?str.length()-1:str.length();
        return str.substring(l,r);
    }


}
